<template>
  <div>
    <Breadcrumb></Breadcrumb>
    <div class="header">
      <div class="form">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="预定编号">
            <el-input v-model="formInline.user" placeholder="预定时间" size="small"></el-input>
          </el-form-item>
          <el-form-item label="预定时间">
            <el-date-picker
              v-model="formInline.date"
              size="small"
              type="date"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="group">
        <el-button size="small" type="warning" @click="orderCollect()">订单汇总</el-button>
        <download-excel
          class="export-excel-wrapper"
          :data="DetailsForm"
          :fields="json_fields"
          :header="title"
          name="xxx公司订单列表.xls">
          <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
          <el-button size="small" type="warning">导出</el-button>
        </download-excel>
      </div>
    </div>
    <div class="content">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        header-cell-class-name="active-header"
        cell-class-name="table-center"
        @select="select"
        @select-all="select">
        <el-table-column type="selection" width="55" :selectable="selectable"> </el-table-column>
        <el-table-column prop="code" label="订单编号">
          <template slot-scope="scope">
            <span
              style="color: blue; cursor: pointer"
              @click="orderDesc(scope.$index, scope.row)"
              >{{ scope.row.code }}</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="ordername" label="下单人" width="120"> </el-table-column>
        <el-table-column prop="company" label="所属单位" width="140"> </el-table-column>
        <el-table-column prop="phone" label="联系电话"> </el-table-column>
        <el-table-column prop="yudingTime" label="预定时间">
          <!-- <template slot-scope="scope">
            {{ dayjs(scope.row.yudingTime).format("YYYY-MM-DD") }}
          </template> -->
        </el-table-column>
        <el-table-column prop="price" label="订单总价格" width="120"> </el-table-column>
        <el-table-column prop="huizongStatus" label="汇总状态" width="150">
          <!-- <template slot-scope="scope">
            {{ scope.row.huizongStatus == 1 ? "未汇总" : "已汇总" }}
          </template> -->
        </el-table-column>
      </el-table>
      <Pagination :total="total" :pageSize="pageSize" @CurrentChange="CurrentChange"></Pagination>
    </div>
    <OrderDesc ref="child"></OrderDesc>
  </div>
</template>

<script>
import dayjs from "dayjs";
import Pagination from "@/components/pagination/Pagination .vue";
import OrderDesc from "./OrderDesc.vue";
export default {
  components: {
    Pagination,
    OrderDesc,
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      total: 10,
      pageSize: 1,
      ids: [],
      DetailsForm: [],
      title: "汇总清单",
      json_fields: {
        订单编号: {
          field: "code",
          callback: (value) => {
            return "&nbsp;" + value;
          },
        },
        下单人: "ordername",
        所属单位: "company",
        联系电话: "phone",
        预定时间: "yudingTime",
        订单总价格: "price",
        汇总状态: "huizongStatus",
      },
    };
  },
  methods: {
    dayjs,
    orderDesc() {
      this.$refs.child.drawer = true;
    },
    orderCollect() {
      if (this.ids.length >= 2) {
        this.$api.changeStatus({ ids: this.ids.join(",") }).then((res) => {
          if (res.data.status == 200) {
            this.$message({
              message: "汇总成功",
              type: "success",
            });
            this.orderList(1);
          }
        });
      } else {
        this.$alert("汇总订单至少需要2条订单信息", "汇总订单", {
          confirmButtonText: "确定",
          callback: (action) => {
            this.$message({
              type: "info",
              message: `取消汇总`,
            });
          },
        });
      }
    },
    select(selection, row) {
      let arr = [];
      selection.forEach((ele) => {
        arr.push(ele.id);
      });
      this.ids = arr;
      this.DetailsForm = selection;
    },
    CurrentChange(page) {
      this.orderList(page);
    },
    selectable(row, index) {
      if (row.huizongStatus == "未汇总") return true;
      else return false;
    },
    async orderList(page) {
      let res = await this.$api.orderList({ page });
      if (res.data.status == 200) {
        let arr = res.data.data;
        arr.forEach((ele) => {
          ele.huizongStatus = ele.huizongStatus == 1 ? "未汇总" : "已汇总";
          ele.yudingTime = dayjs(ele.yudingTime).format("YYYY-MM-DD");
        });
        this.tableData = arr;
        this.total = res.data.total;
        this.pageSize = res.data.pageSize;
      } else {
        this.tableData = [];
        this.total = 1;
        this.pageSize = 1;
      }
    },
  },
  created() {
    this.orderList(1);
  },
};
</script>

<style lang="less" scoped>
.header {
  background-color: #fff;
  margin-bottom: 20px;
  padding: 10px;

  .el-form-item {
    margin-bottom: 16px;
  }

  .group {
    border: 1px solid #eee;
    padding: 8px;
  }
}

.content {
  /deep/ .active-header {
    color: #333;
    text-align: center;
  }

  /deep/ .table-center {
    text-align: center;
  }

  .pagination {
    padding: 10px;
  }
}
.export-excel-wrapper {
  display: inline-block;
  margin-left: 10px;

  button {
    padding-left: 30px;
    padding-right: 30px;
  }
}
</style>
